
package com.comtop.increment.views;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view shows data obtained from the model. The sample creates a dummy model
 * on the fly, but a real implementation would connect to the model available either in this or another plug-in (e.g., the workspace). The view is
 * connected to the model using a content provider. The view uses a label provider to define how model objects should be presented in the view. Each
 * view can present the same model objects using different labels and icons, if needed. Alternatively, a single label provider can be shared between
 * views in order to ensure that objects of the same type are presented in the same way everywhere.
 */
public class FavoritesView extends ViewPart {
    
    private TableViewer viewer;
    
    /*
     * The content provider class is responsible for providing objects to the view. It can wrap existing objects in adapters or simply return objects
     * as-is. These objects may be sensitive to the current input of the view, or ignore it and always show the same content (Task List, for example).
     */

    class ViewContentProvider implements IStructuredContentProvider {
        
        public void inputChanged(Viewer v, Object oldInput, Object newInput) {
        }
        
        public void dispose() {
        }
        
        public Object[] getElements(Object parent) {
            return new String[] { "One", "Two", "Three" };
        }
    }
    
    /*
     * The label provider class is responsible for translating objects into text and images that are displayed in the various cells of the table.
     */
    class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
        
        public String getColumnText(Object obj, int index) {
            return getText(obj);
        }
        
        public Image getColumnImage(Object obj, int index) {
            return getImage(obj);
        }
        
        @Override
        public Image getImage(Object obj) {
            return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
        }
    }
    
    /**
     * The constructor.
     */
    public FavoritesView() {
    }
    
    /**
     * This is a callback that will allow us to create the viewer and initialize it.
     */
    @Override
    public void createPartControl(Composite parent) {
        viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        viewer.setContentProvider(new ViewContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        viewer.setInput(getViewSite());
    }
    
    public void showMessage(String message) {
        MessageDialog.openInformation(viewer.getControl().getShell(), "Favorites", message);
    }
    
    /**
     * Passing the focus request to the viewer's control.
     */
    @Override
    public void setFocus() {
        viewer.getControl().setFocus();
    }
}
